/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.qiujuer.genius.ui.element;

import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.render.Path;
import ohos.agp.utils.Point;
import ohos.agp.utils.Rect;
import ohos.miscservices.timeutility.Time;

/**
 * Have a circle animation
 *
 * @since 2021-05-10
 */
public class RipAnimElement extends RipElement implements Animatable {
    // Time
    private static final int ALL_DURATION = ANIMATION_DURATION * 2;
    private Point mPoint = new Point();
    private float mRadius;
    private float mMaxRadius;
    private long mStartTime;
    private boolean isFirst = true;
    private boolean isRun = false;

    /**
     * constructor
     */
    public RipAnimElement() {
        setCallback(element -> onBoundsChange(element.getBounds()));
    }

    @Override
    protected void draw(Canvas canvas, Path path, Paint paint) {
        if (isFirst) {
            isFirst = false;
            start();
        } else {
            int sc = canvas.save();
            canvas.clipPath(path,Canvas.ClipOp.INTERSECT);
            canvas.drawCircle(mPoint.getPointX(), mPoint.getPointY(), mRadius, paint);
            canvas.restoreToCount(sc);
        }
    }

    @Override
    protected void onBoundsChange(Rect bounds) {
        super.onBoundsChange(bounds);
        mPoint.modify(bounds.left, bounds.top);

        int x = bounds.right - bounds.left;
        int y = bounds.bottom - bounds.top;

        mMaxRadius = (float) Math.sqrt(x * x + y * y);
        mRadius = mMaxRadius;
    }

    /**
     * update animation radius by factor
     *
     * @param factor effect the animation
     */
    protected void onInAnimateUpdate(float factor) {
        mRadius = mMaxRadius * factor;
        invalidateSelf();
    }

    @Override
    public void setBounds(int left, int top, int right, int bottom) {
        super.setBounds(left, top, right, bottom);
    }

    @Override
    public void start() {
        if (isRun) {
            unscheduleSelf(mAnim);
        }
        isRun = true;
        mStartTime = Time.getRealActiveTime() + FRAME_DURATION * 6;
        scheduleSelf(mAnim, mStartTime);
    }

    @Override
    public void stop() {
        unscheduleSelf(mAnim);
    }

    @Override
    public boolean isRunning() {
        return isRun;
    }

    private final Runnable mAnim = new Runnable() {
        @Override
        public void run() {
            long currentTime = Time.getRealActiveTime();
            long diff = currentTime - mStartTime;
            int duration = ALL_DURATION;
            if (diff <= duration) {
                float interpolation = getInterpolation((float) diff / (float) duration);
                onInAnimateUpdate(interpolation);
                scheduleSelf(this, currentTime + FRAME_DURATION);
            } else {
                unscheduleSelf(this);

                // Notify
                onInAnimateUpdate(1f);

                isRun = false;
            }
        }
    };
}
